﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;


namespace cecbClasses.Data_Adapters.ImpactAndActivity
{
    public class Func_Activities
    {
        public Func_Activities()
        {
            dataContext = new DSSEntities();
        }

        DSSEntities dataContext;

        public Boolean insertIntoActivities(Data_Activities arg)
        {
            Boolean isInserted = false;

            try
            {
                Activities activityObj = new Activities();
                activityObj.actv_name = arg.actv_name;
                activityObj.actv_phase = arg.Actv_phase;
                activityObj.actv_reference = arg.Actv_reference;
                activityObj.actv_status = arg.Actv_status;

                dataContext.AddToActivities(activityObj);
                dataContext.SaveChanges();

                isInserted = true;
            }
            catch (Exception)
            {

                isInserted = false;
            }

            return isInserted;
        }
        
        private String getActivityReference()
        {
            int sub;

            try
            {
                var idKey = (from c in dataContext.Activities
                             orderby c.actv_id descending
                             select c).First();


                String id = idKey.actv_reference.ToString();

                sub = int.Parse(id.Substring(4)) + 1;
            }
            catch (Exception)
            {
                sub = 1;
            }
            String newId = "ACTV" + sub;
            return newId.ToString();

        }
        public String insertNewActivity(String actv, String phase, String proRef)
        {
            Activities actvity;
            String reference = String.Empty;
            try
            {
                reference = getActivityReference();
                actvity = new Activities();
                actvity.actv_reference = reference;
                actvity.actv_phase = phase;
                actvity.actv_name = actv;
                actvity.actv_status = true;

                dataContext.AddToActivities(actvity);
                dataContext.SaveChanges();

                return reference;
            }
            catch (Exception)
            {

                return String.Empty;
            }



        }

        public String getActivityNameById(String id)
        {

            var idKey = (from c in dataContext.Activities
                         where c.actv_reference == id
                         select c).First();


            return idKey.actv_name.ToString();

        }

        public List<Activities> getAllActivities()
        {
             List<Activities> actList;
             try
             {
                 IQueryable<Activities> query = from c in dataContext.Activities
                                                select c;

                 actList = query.ToList();
             }
             catch (NullReferenceException)
             {

                 actList = null;
             }
             catch (Exception) 
             { 
                 throw;
             } 

            return actList;
        }

        public List<Activities> getActivityFromProjectActivity(String proRef, String actName)
        {
             
            List<Activities> selectedImpactList = null;
            try
            {
                 
                IQueryable<Activities> query = from c in dataContext.ProjectActivities
                                               join b in dataContext.Activities on c.actv_reference equals b.actv_reference
                                               where c.proj_reference == proRef && b.actv_name.Contains(actName)                                                
                                               select b;

                selectedImpactList = query.ToList();
            }
            catch (Exception)
            {
                selectedImpactList = null;

            }
            return selectedImpactList;
        }

        public List<Activities> getActivitiesByName(String name)
        {
           List<Activities> selectedActivities = null;
            try
            {
               IQueryable<Activities> query  =  from c in dataContext.Activities
                                                where c.actv_name.Contains(name)
                                                select c;

                selectedActivities = query.ToList();
            }
            catch (Exception)
            {

                selectedActivities = null;
            }

            return selectedActivities;
        }

        public List<Activities> getActivityDetailsByName(String name, Projects proj, Boolean isAll)
        {
            IQueryable<Activities> query;
            if (isAll)
            {
                query = from c in dataContext.Activities
                        where c.actv_name.Contains(name)
                        select c;
            }
            else
            {

                query = from c in dataContext.Activities
                        where (!dataContext.ProjectActivities.Any(p => p.actv_reference == c.actv_reference)) 
                                && 
                                (dataContext.ProjectActivities.Any(r => r.proj_reference == proj.proj_reference && c.actv_name.Contains(name)))
                        select c;
            }

            List<Activities> selectedComponent = query.ToList();

            return selectedComponent;

        }

        public List<ProjectActivities> getProjectActivityDetailsByName(String name, Projects proj )
        {
            IQueryable<ProjectActivities> query;


            query = from c in dataContext.Activities
                    join d in dataContext.ProjectActivities on c.actv_reference equals d.actv_reference
                    where d.proj_reference == proj.proj_reference && c.actv_name.Contains(name)
                    select d;


            List<ProjectActivities> selectedComponent = query.ToList();

            return selectedComponent;

        }


        public bool insertActivity(List<String> phase, List<String> act, List<String> actRef, int count, String proRef)
        {
            int itemNo = 0;
            bool isInserted = false;
             
            try
            {
                while (count > 0)
                { 
                    ProjectActivities pact = new ProjectActivities();
                    pact.proj_reference = proRef;
                    pact.actv_reference = actRef[itemNo];
                    pact.actv_addedBy = String.Empty;
                    pact.actv_approvedBy = String.Empty;
                    pact.proActiv_status = true;
                    dataContext.AddToProjectActivities(pact);
                    dataContext.SaveChanges();

                    itemNo++;
                    count--;
 
                }


                isInserted = true;
            }
            catch (Exception)
            {

                isInserted = false;
            }


            return isInserted;
        }
        
        private void isValideToTemplate(String pRef, String acid, String imid)
        {

            var ptypevar = from c in dataContext.Projects
                           where c.proj_reference == pRef
                           select c.proj_type;

            var pcatvar = from p in dataContext.Projects
                          where p.proj_reference == pRef
                          select p.proj_category;

            string ptype = ptypevar.ToString();
            string pcat = pcatvar.ToString();

            var count = from o in dataContext.Templates
                        where o.proj_typeref == ptype && o.actref == acid && o.impt == imid
                        select o;

            if (count.ToList().Count == 0)
            {

                Templates temp = new Templates();
                temp.proj_typeref = ptype;
                temp.proj_catref = pcat;
                temp.actref = acid;
                temp.impt = imid;
                dataContext.AddToTemplates(temp);
                dataContext.SaveChanges();

            }

        }
        
        public bool updateActivity(string activityRef, string activityName, string activityPhase)
        {
            bool isupdated = false;

            try
            {
                Activities activityObject = dataContext.Activities.First(i => i.actv_reference == activityRef);                
                activityObject.actv_name = activityName;
                dataContext.SaveChanges();
                isupdated = true;
            }
            catch (Exception)
            {
                isupdated = false;
            }

            return isupdated;

        }

        public bool insertProjectActivities(String proRef, String actRef)
        { 
            try
            {
                ProjectActivities pact = new ProjectActivities();
                pact.proj_reference = proRef;
                pact.actv_reference = actRef;
                pact.actv_addedBy = String.Empty;
                pact.actv_approvedBy = String.Empty;
                pact.proActiv_status = true;
                dataContext.AddToProjectActivities(pact);
                dataContext.SaveChanges();
                return true;
            }
            catch (Exception)
            { 
                return false;
            }

             
        }
        
        public bool deleteProjectActivity(ProjectActivities projAc, Boolean isCompleteRemove)
        {

            try
            {
                if (isCompleteRemove)
                {
                    List<LeopoldMatrixValues> leoObj = (from c in dataContext.LeopoldMatrixValues
                                                     where c.proj_reference.Equals(projAc.proj_reference) && c.actv_reference.Equals(projAc.actv_reference)
                                                     select c).ToList();
                    foreach (LeopoldMatrixValues l in leoObj)
                    {

                        dataContext.LeopoldMatrixValues.DeleteObject(l);
                        dataContext.SaveChanges();
                    }

                    List<LeopoldMatrixValuesExtra> leox = (from c in dataContext.LeopoldMatrixValuesExtra
                                                           where c.proj_reference.Equals(projAc.proj_reference) && c.actv_reference.Equals(projAc.actv_reference)
                                                           select c).ToList();
                    foreach (LeopoldMatrixValuesExtra l in leox)
                    {

                        dataContext.LeopoldMatrixValuesExtra.DeleteObject(l);
                        dataContext.SaveChanges();
                    }

                    ProjectActivities pim = dataContext.ProjectActivities.First(i => i.actv_reference.Equals(projAc.actv_reference) && i.proj_reference.Equals(projAc.proj_reference));
                    dataContext.ProjectActivities.DeleteObject(pim);
                    dataContext.SaveChanges();
                }
                else
                {
                    ProjectActivities pim = dataContext.ProjectActivities.First(i => i.actv_reference.Equals(projAc.actv_reference) && i.proj_reference.Equals(projAc.proj_reference));
                    dataContext.ProjectActivities.DeleteObject(pim);
                    dataContext.SaveChanges();
                }
                return true;
            }
            catch (System.Data.UpdateException  )
            { 
                return false;
            }

        }

        public Boolean checkForDeleteIsEmailsSendByProject(ProjectActivities proj)
        {

            Projects val = dataContext.Projects.First(c => c.proj_reference.Equals(proj.proj_reference));

            if (val.proj_isEmailsSend == false)
                return true;
            else
                return false;

        }
    }
}
